package com.millstein.tsinglog.controller;

import com.millstein.tsinglog.annotation.CommonAccessLog;
import com.millstein.tsinglog.commons.constant.SystemConstant;
import com.millstein.tsinglog.commons.enums.ResultCode;
import com.millstein.tsinglog.exception.TsinglogException;
import com.millstein.tsinglog.model.pojo.*;
import com.millstein.tsinglog.model.vo.biz.ArticleVo;
import com.millstein.tsinglog.model.vo.biz.CategoryVo;
import com.millstein.tsinglog.model.vo.result.PageDetail;
import com.millstein.tsinglog.model.vo.result.PageResult;
import com.millstein.tsinglog.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class AccessController extends BaseController{

    @Autowired
    private ArticleService articleService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 进入首页调用的方法
     * @param model
     * @return
     */
    @RequestMapping("/index")
    @CommonAccessLog("进入首页")
    public String toIndex(Model model) {

        super.getCommonDataForPortal(model);

        // 给页面准备文章最近更新的10篇文章的数据
        PageResult<ArticleVo> latestReleaseArticlePage = articleService.getArticleForLatest(SystemConstant.DEFAULT_CURRENT_PAGE,
                SystemConstant.DEFAULT_PAGE_SIZE_FOR_INDEX, SystemConstant.INDEX_TYPE_RELEASE);
        // 给页面准备最近评论的10篇文章的数据
        PageResult<ArticleVo> latestReplyArticlePage = articleService.getArticleForLatest(SystemConstant.DEFAULT_CURRENT_PAGE,
                SystemConstant.DEFAULT_PAGE_SIZE_FOR_INDEX, SystemConstant.INDEX_TYPE_REPLY);
        // 给页面准备轮播图时长的数据
        Integer carouselInterval = Integer.parseInt((String) carouselService.getConfigByKey("interval"));
        // 给页面准备轮播图的数据
        List<CarouselImage> carouselImageList = carouselService.getCarouselImageListForPortal();

        model.addAttribute("latestReleaseArticlePage", latestReleaseArticlePage);
        model.addAttribute("latestReplyArticlePage", latestReplyArticlePage);
        model.addAttribute("carouselInterval", carouselInterval);
        if (!CollectionUtils.isEmpty(carouselImageList)) {
            model.addAttribute("carouselImageList", carouselImageList);
        }

        return "index";
    }

    @RequestMapping("/list")
    @CommonAccessLog("进入文章列表页")
    public String toList(Model model,
                         @RequestParam(value = "page", required = false) Integer currentPage,
                         Long cid,
                         Long tid,
                         String keyword) {

        super.getCommonDataForPortal(model);

        PageDetail<ArticleVo> latestArticlePage;
        // 给页面准备最近更新的20篇文章的数据
        try{
            latestArticlePage = articleService.getArticleForSearch(currentPage,
                    SystemConstant.DEFAULT_PAGE_SIZE_FOR_LIST, cid, tid, keyword);
        } catch (TsinglogException e) {
            log.error("发生异常：{}", e.getMessage(), e);
            model.addAttribute("code", e.getResultCode().getCode());
            return "error/500";
        }
        // 给页面准备分类信息
        List<CategoryVo> categoryList = categoryService.getAllCategoryForPortal();;

        model.addAttribute("latestArticlePage", latestArticlePage);
        model.addAttribute("categoryList", categoryList);

        return "list";
    }

    @RequestMapping("/article/{id}")
    @CommonAccessLog("进入文章[{2}]详情页")
    public String toArticle(Model model, @PathVariable("id") Long id) {

        super.getCommonDataForPortal(model);

        // 给页面准备该文章的内容
        ArticleVo article = new ArticleVo();
        try {
            article = articleService.getArticleDetailForPortal(id);
        } catch (TsinglogException e) {
            model.addAttribute("code", e.getResultCode().getCode());
            if (e.getResultCode() != ResultCode.INSERT_BROWSE_COUNT_ERROR) {
                return "error/500";
            }
        }
        // 给页面准备上一篇文章的信息
        Map<String, Object> preArticleMap = articleService.getArticleByPosition(id, -1);
        // 给页面准备下一篇文章的信息
        Map<String, Object> nextArticleMap = articleService.getArticleByPosition(id, 1);
        // 给页面准备该文章所属的标签信息
        List<Tag> tagList = tagService.getTagInfoByTids(article.getTags());

        model.addAttribute("article", article);
        model.addAttribute("preArticle", preArticleMap);
        model.addAttribute("nextArticle", nextArticleMap);
        model.addAttribute("tagList", tagList);


        return "article";
    }

    /**
     * 进入首页调用的方法
     * @param model
     * @return
     */
    @RequestMapping("/about")
    @CommonAccessLog("进入关于本站页")
    public String toAbout(Model model) {

        super.getCommonDataForPortal(model);

        return "about";
    }

    /**
     * 进入首页调用的方法
     * @param model
     * @return
     */
    @RequestMapping("/guestBook")
    @CommonAccessLog("进入留言板")
    public String toGuestBook(Model model) {

        super.getCommonDataForPortal(model);

        return "guestBook";
    }
}
